/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dto.TourDetailDTO;
import east2west.service.TourService;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author anhtuan
 */
public class NewTourServiceAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String ERR = "err";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        boolean flag = true;
        javax.servlet.ServletConfig config = servlet.getServletConfig();
        TourService serTour = new TourService();

        TourDetailDTO tourDetail = new TourDetailDTO();

        //upload anhle = new ArrayList<SchedulesDTO>();
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
        } else {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = null;
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            Iterator itr = items.iterator();
            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                if (item.isFormField()) {
                    String name = item.getFieldName();

                    //detailDTO
                    if (name.equals("service")) {
                        int service = Integer.parseInt(item.getString());
                        tourDetail.setTourServiceId(service);
                    }
                    if (name.equals("tourId")) {
                        tourDetail.setTourId(Integer.parseInt(item.getString()));
                    }
                    if (name.equals("detail")) {
                        tourDetail.setDetail(item.getString());
                    }
                    if (name.equals("priceser")) {
                        float priceser = Float.parseFloat(item.getString());
                        tourDetail.setPrice(priceser);

                    }

                } else {
                    try {
                        String itemName = item.getName();
                        tourDetail.setImagesUrl("images/Tour/" + itemName);
                        String url = config.getServletContext().getRealPath("/");
                        String url1 = url.substring(0, (url.length() - 10));
                        File savedFile = new File(url1 + "/web/images/Tour/" + itemName);
                        item.write(savedFile);
                        //request.setAttribute("url", url1);

                    } catch (Exception e) {
                        flag = false;
                    }
                }
            }
        }
        if (!serTour.createTourDetail(tourDetail)) {
            flag = false;
        }
        if (flag) {
            request.setAttribute("tourId", tourDetail.getTourId());
            return mapping.findForward(SUCCESS);
        } else {
            return mapping.findForward(ERR);
        }

    }
}
